/**
 * @PROJECT 
 * @DATE 2018年7月11日 下午8:55:46
 * @AUTHOR LUYU
 */
package com.kool.system.action.role;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.core.base.IBaseAction;
import com.kool.core.exception.AppException;
import com.kool.core.util.BeanUtils;
import com.kool.system.bean.SyRoleRoleGroupBean;
import com.kool.system.bean.io.role.SYJOINGROUPXBean;
import com.kool.system.service.RoleService;

/**
 * @DESCRIBE 加入角色组
 * @AUTHOR LUYU
 * @DATE 2018年7月11日 下午8:55:46
 *
 */
@Controller
public class JoinGroupAction implements IBaseAction {
	@Autowired
	private RoleService service;

	@Override
	@ResponseBody
	@RequestMapping(value = "/sys/role/joinGroup", method = RequestMethod.POST)
	public JSONObject doAction(@RequestBody String sInput) throws AppException {
		JSONObject pkgIn = JSONObject.parseObject(sInput);
		JSONArray secRole = pkgIn.getJSONArray("SYROLECODEX");
		JSONObject propRole = secRole.getJSONObject(0);
		String roleCode = propRole.getString("roleCode");

		JSONArray secGroup = pkgIn.getJSONArray("SYJOINGROUPX");
		List<SyRoleRoleGroupBean> listIn = new ArrayList<>();
		for (int i = 0; i < secGroup.size(); i++) {
			SYJOINGROUPXBean io = JSON.toJavaObject(secGroup.getJSONObject(i), SYJOINGROUPXBean.class);
			SyRoleRoleGroupBean roleGroup = (SyRoleRoleGroupBean) BeanUtils.io2Bean(io, SyRoleRoleGroupBean.class,
					"srr");
			listIn.add(roleGroup);
		}
		service.joinGroup(roleCode, listIn);

		return null;
	}

}
